/*
* 设备详情
* */

import 'package:center_beijing/api/authorization_api.dart';
import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/enums.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/common/utils.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/config/routeConfig.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:center_beijing/widgets/imagesWatch_widget2.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tab_indicator_styler/tab_indicator_styler.dart';

class EquipmentDetail extends StatefulWidget {
  @override
  _EquipmentDetailState createState() => _EquipmentDetailState();
}

class _EquipmentDetailState extends State<EquipmentDetail>
    with SingleTickerProviderStateMixin {
  final List<Tab> myTabs = <Tab>[
    Tab(text: '使用记录'),
    Tab(text: '保养记录'),
    Tab(text: '维修记录'),
    Tab(text: '资质证书'),
  ];
  AnimationController _controller;
  CustomLoading loading = CustomLoading.withMessage('请稍候...');
  var compId = Get.arguments;
  var equipment = Map();
  var statusList = [];
  TabController _tabController;
  int currentIndex = 0;
  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.getAllPunishType();
  }

  @override
  void dispose() {
    _controller.dispose();
    loading.dismiss();
    super.dispose();
  }

  getStatus(value) {
    var bbq = '';
    statusList.forEach((element) {
      if (value == element['code']) {
        bbq = element['name'];
      }
    });
    return bbq;
  }

  /*
  * 获取字典类型
  * */
  getAllPunishType() async {
    loading.show(delay: 30);
    var result = await getPunishType({'codeType': 'JX-STATE'});
    if (result != null) {
      statusList = result;
    }
    this.getEquipmentDetail();
    LogUtil.d(result);
  }

  getEquipmentDetail() async {
    var pass = {'id': compId};
    var result = await getMachineEmployeeRelEx(pass);
    loading.dismiss();
    if (result != null && result['errorMessage'] == null) {
      this.setState(() {
        equipment = result;
      });
    }
  }

  Future getAllInfo()async{
    Future.wait([this.getMachineEmployee()]).then((value) {

    });
  }

  Future getMachineEmployee(){
    var pass = {
'machineId':''
    };
    var result = getMachineEmployeeRelEx(pass);
    return result;
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('设备详情'),
      ),
      body: Container(
        color: Styles.MainBackgroundColor,
        padding: EdgeInsets.fromLTRB(
            0, 0, 0, MediaQuery.of(context).padding.bottom + 70),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("设备编号  ${equipment['code']}"),
              ),
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("设备名称  ${equipment['equipmentName']}"),
              ),
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("入场时间  ${equipment['entryDateStr']}"),
              ),
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("责任人  ${equipment['contactPerson']}"),
              ),
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("联系电话  ${equipment['contactPersonTel']}"),
              ),
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("关联人员  ${equipment['employeeRels'].map((item){
                  return item['personName'];
                }).toList().join(',')}"),
              ),
              Container(
                padding: EdgeInsets.all(Styles.spacing8),
                child: Text("状态  ${this.getStatus(equipment['status'])}"),
              ),
              Container(
                padding: EdgeInsets.fromLTRB(Styles.spacing8, Styles.spacing8,
                    Styles.spacing8, Styles.spacing8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Container(
                          height: 26.h,
                          width: 4,
                          color: Styles.BlueColor,
                          margin: EdgeInsets.only(right: Styles.spacing8),
                        ),
                        Text(
                          '保养维修记录',
                          style: TextStyle(
                              color: Styles.DetailColor,
                              fontSize: Styles.Middle16Font),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Container(
                height: 40.h,
                color: Colors.white,
                child: DefaultTabController(
                  length: 4,
                  initialIndex: currentIndex,
                  child: TabBar(
                    controller: _tabController,
                    onTap: (value) {
                      this.setState(() {
                        currentIndex = value;
                      });
                    },
                    indicatorColor: Colors.red,
                    tabs: myTabs,
                    labelColor: Styles.BlueColor,
                    unselectedLabelColor: Styles.LeftTitleColor,
                    // add it here
                    indicator: MaterialIndicator(
                        color: Styles.BlueColor, height: 1),
                  ),
                ),
              ),
              currentIndex==0?(equipment['useRecords'] != null)
                  ? Column(
                children: equipment['useRecords'].map<Widget>((item) {
                  return Container(
                    padding: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    margin: EdgeInsets.fromLTRB(
                        0, Styles.spacing4, 0, Styles.spacing4),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                            child: Container(
                              padding: EdgeInsets.all(Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8, 0, Styles.spacing8, 0),
                              decoration: BoxDecoration(
                                  color: Styles.GrayLight,
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(4.0.r)),
                                  border: Border.all(
                                      width: 1, color: Styles.LineColor)),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                          '由${item['operatorId']}在${item['startTimeStr']}操作',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize: Styles.NormalFont)),
                                      GestureDetector(
                                        onTap: () {
                                          this.setState(() {
                                            item['hide'] =
                                            item['hide'] == null
                                                ? false
                                                : !item['hide'];
                                          });
                                        },
                                        child: Container(
                                          padding: EdgeInsets.only(
                                              right: Styles.spacing8),
                                          child: Text(
                                            (item['hide'] == null ||
                                                item['hide'])
                                                ? '查看'
                                                : '收起',
                                            style: TextStyle(
                                                color: Styles.BlueColor,
                                                fontSize: Styles.NormalFont),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  item['hide'] != null && !item['hide']
                                      ? Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0,
                                            Styles.spacing8,
                                            0,
                                            Styles.spacing8),
                                        child: Text(
                                          '使用情况',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          item['workContent'],
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      )
                                    ],
                                  )
                                      : Container(),
                                ],
                              ),
                            ))
                      ],
                    ),
                  );
                }).toList(),
              )
                  : Container(child: Text('暂无数据'),):Container(),
              currentIndex==1?(equipment['records'] != null)
                  ? Column(
                children: equipment['records'].map<Widget>((item) {
                  return Container(
                    padding: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    margin: EdgeInsets.fromLTRB(
                        0, Styles.spacing4, 0, Styles.spacing4),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          padding: EdgeInsets.all(Styles.spacing8),
                          decoration: BoxDecoration(
                            color: StatusType.getOperationColor(
                                item['maintenanceType']),
                            shape: BoxShape.circle,
                          ),
                          child: Text(
                            StatusType.getOperationStr(
                                item['maintenanceType']),
                            style: TextStyle(
                                color: Styles.ShareWhite,
                                fontSize: Styles.NormalFont),
                          ),
                        ),
                        Expanded(
                            child: Container(
                              padding: EdgeInsets.all(Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8, 0, Styles.spacing8, 0),
                              decoration: BoxDecoration(
                                  color: Styles.GrayLight,
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(4.0.r)),
                                  border: Border.all(
                                      width: 1, color: Styles.LineColor)),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                          '由${item['personInCharge']}在${item['maintenanceDateStr']}操作',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize: Styles.NormalFont)),
                                      GestureDetector(
                                        onTap: () {
                                          this.setState(() {
                                            item['hide'] =
                                            item['hide'] == null
                                                ? false
                                                : !item['hide'];
                                          });
                                        },
                                        child: Container(
                                          padding: EdgeInsets.only(
                                              right: Styles.spacing8),
                                          child: Text(
                                            (item['hide'] == null ||
                                                item['hide'])
                                                ? '查看'
                                                : '收起',
                                            style: TextStyle(
                                                color: Styles.BlueColor,
                                                fontSize: Styles.NormalFont),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  item['hide'] != null && !item['hide']
                                      ? Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0,
                                            Styles.spacing8,
                                            0,
                                            Styles.spacing8),
                                        child: Text(
                                          '检查情况',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          item['maintenanceContent'],
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      item['attachments'] != null &&
                                          item['attachments']
                                              .length >
                                              0
                                          ? Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment
                                            .start,
                                        children: [
                                          Container(
                                            padding: EdgeInsets
                                                .fromLTRB(
                                                0,
                                                Styles
                                                    .spacing8,
                                                0,
                                                Styles
                                                    .spacing8),
                                            child: Text(
                                              '现场照片',
                                              style: TextStyle(
                                                  color: Styles
                                                      .DetailColor,
                                                  fontSize: Styles
                                                      .NormalFont),
                                            ),
                                          ),
                                          ImagesWatchWidget2(
                                            photos: item[
                                            'attachments']
                                                .map<String>(
                                                    (image) {
                                                  return "${GlobalConfig.baseUrl}fs/irest/file/mps_fileTransferHandler/image?id=${image['fileId']}&ZJQFSESSION=${GlobalConfig.session}";
                                                }).toList(),
                                          )
                                        ],
                                      )
                                          : Container(),
                                    ],
                                  )
                                      : Container(),
                                ],
                              ),
                            ))
                      ],
                    ),
                  );
                }).toList(),
              )
                  : Container(child: Text('暂无数据'),):Container(),
              currentIndex==2?(equipment['repairExs'] != null)
                  ? Column(
                children: equipment['repairExs'].map<Widget>((item) {
                  return Container(
                    padding: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    margin: EdgeInsets.fromLTRB(
                        0, Styles.spacing4, 0, Styles.spacing4),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                            child: Container(
                              padding: EdgeInsets.all(Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8, 0, Styles.spacing8, 0),
                              decoration: BoxDecoration(
                                  color: Styles.GrayLight,
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(4.0.r)),
                                  border: Border.all(
                                      width: 1, color: Styles.LineColor)),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                          '由${item['repairUserId']}在${item['approachDateStr']}维修',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize: Styles.NormalFont)),
                                      GestureDetector(
                                        onTap: () {
                                          this.setState(() {
                                            item['hide'] =
                                            item['hide'] == null
                                                ? false
                                                : !item['hide'];
                                          });
                                        },
                                        child: Container(
                                          padding: EdgeInsets.only(
                                              right: Styles.spacing8),
                                          child: Text(
                                            (item['hide'] == null ||
                                                item['hide'])
                                                ? '查看'
                                                : '收起',
                                            style: TextStyle(
                                                color: Styles.BlueColor,
                                                fontSize: Styles.NormalFont),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  item['hide'] != null && !item['hide']
                                      ? Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0,
                                            Styles.spacing8,
                                            0,
                                            Styles.spacing8),
                                        child: Text(
                                          '维修情况',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "承修单位 ${item['repairComp']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "检验人 ${item['checkUserId']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "接机人 ${item['acceptUserId']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0,
                                            Styles.spacing8,
                                            0,
                                            Styles.spacing8),
                                        child: Text(
                                          '维修结果',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          item['questionAfter'],
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                    ],
                                  )
                                      : Container(),
                                ],
                              ),
                            ))
                      ],
                    ),
                  );
                }).toList(),
              )
                  : Container(child: Text('暂无数据'),):Container(),
              currentIndex==3?(equipment['certificates'] != null)
                  ? Column(
                children: equipment['certificates'].map<Widget>((item) {
                  return Container(
                    padding: EdgeInsets.fromLTRB(
                        Styles.spacing8, 0, Styles.spacing8, 0),
                    margin: EdgeInsets.fromLTRB(
                        0, Styles.spacing4, 0, Styles.spacing4),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                            child: Container(
                              padding: EdgeInsets.all(Styles.spacing8),
                              margin: EdgeInsets.fromLTRB(
                                  Styles.spacing8, 0, Styles.spacing8, 0),
                              decoration: BoxDecoration(
                                  color: Styles.GrayLight,
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(4.0.r)),
                                  border: Border.all(
                                      width: 1, color: Styles.LineColor)),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                          item['certificationName']??'--',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize: Styles.NormalFont)),
                                      GestureDetector(
                                        onTap: () {
                                          this.setState(() {
                                            item['hide'] =
                                            item['hide'] == null
                                                ? false
                                                : !item['hide'];
                                          });
                                        },
                                        child: Container(
                                          padding: EdgeInsets.only(
                                              right: Styles.spacing8),
                                          child: Text(
                                            (item['hide'] == null ||
                                                item['hide'])
                                                ? '查看'
                                                : '收起',
                                            style: TextStyle(
                                                color: Styles.BlueColor,
                                                fontSize: Styles.NormalFont),
                                          ),
                                        ),
                                      )
                                    ],
                                  ),
                                  item['hide'] != null && !item['hide']
                                      ? Column(
                                    crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0,
                                            Styles.spacing8,
                                            0,
                                            Styles.spacing8),
                                        child: Text(
                                          '证书情况',
                                          style: TextStyle(
                                              color: Styles.DetailColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "监检证书号 ${item['certificationCode']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "发证单位 ${item['certificationOrg']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "出厂日期 ${item['outDateStr']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "复审日期 ${item['checkDateStr']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      Container(
                                        padding: EdgeInsets.fromLTRB(
                                            0, 0, 0, Styles.spacing8),
                                        child: Text(
                                          "备注 ${item['remark']}",
                                          style: TextStyle(
                                              color:
                                              Styles.LeftTitleColor,
                                              fontSize:
                                              Styles.NormalFont),
                                        ),
                                      ),
                                      item['list'] != null &&
                                          item['list']
                                              .length >
                                              0
                                          ? Column(
                                        crossAxisAlignment:
                                        CrossAxisAlignment
                                            .start,
                                        children: [
                                          Container(
                                            padding: EdgeInsets
                                                .fromLTRB(
                                                0,
                                                Styles
                                                    .spacing8,
                                                0,
                                                Styles
                                                    .spacing8),
                                            child: Text(
                                              '证书照片',
                                              style: TextStyle(
                                                  color: Styles
                                                      .DetailColor,
                                                  fontSize: Styles
                                                      .NormalFont),
                                            ),
                                          ),
                                          ImagesWatchWidget2(
                                            photos: item[
                                            'list']
                                                .map<String>(
                                                    (image) {
                                                  return "${GlobalConfig.baseUrl}fs/irest/file/mps_fileTransferHandler/image?id=${image['fileId']}&ZJQFSESSION=${GlobalConfig.session}";
                                                }).toList(),
                                          )
                                        ],
                                      )
                                          : Container(),
                                    ],
                                  )
                                      : Container(),

                                ],
                              ),
                            ))
                      ],
                    ),
                  );
                }).toList(),
              )
                  : Container(child: Text('暂无数据'),):Container(),
            ],
          ),
        ),
      ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: Container(
          padding: EdgeInsets.fromLTRB(
              Styles.spacing8, Styles.spacing8, Styles.spacing8, 10),
          child: Container(
            decoration: new BoxDecoration(
              //背景
              color: Styles.BlueColor,
              //设置四周圆角 角度
              borderRadius: BorderRadius.all(Radius.circular(12.0)),
            ),
            child: FlatButton(
              disabledColor: Styles.SelectColor,
              onPressed: () {
                Get.offNamed(RouteConfig.equipmentProblemReport,arguments: equipment);
              },
              child: Container(
                height: Styles.MainButtonHeight,
                alignment: Alignment.center,
                child: Text(
                  '设备养护/维修',
                  style: TextStyle(
                      fontSize: Styles.MainButtonTextFont,
                      color: Colors.white),
                ),
              ),
            ),
          ),
        )
    );
  }
}
